﻿using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace ContactManager.Helpers
{
    public class TextBoxForOptions<VIEWMODEL, TProperty> : IControlForOptions where VIEWMODEL : class
    {
        private readonly Expression<Func<VIEWMODEL, TProperty>> _expression;
        private readonly HtmlHelper<VIEWMODEL> _helper;
        /// <summary>
        /// Initializes a new instance of the <see cref="TextBoxForOptions&lt;VIEWMODEL, TProperty&gt;"/> class.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="expression">The action we will use </param>
        public TextBoxForOptions(HtmlHelper<VIEWMODEL> helper, Expression<Func<VIEWMODEL, TProperty>> expression)
        {
            _helper = helper;
            _expression = expression;
        }
        /// <summary>
        /// Based upon our predicate condition we will either render out a textbox or a readonly textbox
        /// </summary>
        /// <param name="predicate">The condition we wish to test.</param>
        /// <returns>Either a normal textbox or an readonly textbox</returns>
        public string MakeReadOnlyIf(Func<bool> predicate)
        {
            var makeReadOnly = predicate();
            if (makeReadOnly)
            {
                return _helper.TextBoxFor(_expression, new { ReadOnly = true }).ToString();
            }

            return _helper.TextBoxFor(_expression, null).ToString();
        }

    }
}